trigger RoutingPlanChangeOwner on Routing_Plan_And_Visit__c (before update) {
   // Profile prf = [Select p.Name, p.Id From Profile p where Name='System Administrator' or Name='系统管理员'  LIMIT 1 ];
   // if (prf.Id != UserInfo.getProfileId())
   // {
        Routing_Plan_And_Visit__c acc=trigger.new[0];
        //get current user
        //User user;//=[select Id,Name,Title__c from User where Id=:UserInfo.getUserId()];//ASM/DSM  
        //User user2;//=[select Id,Name,ManagerId,Title__c from User where Id=:acc.OwnerId];//TSR
       
       /* List<User> UserList=[select Id,Name,Title__c,ManagerId from User where Id=:UserInfo.getUserId() or Id=:acc.OwnerId];
        for(User uT:UserList)
        {
            if(uT.Id==UserInfo.getUserId())
                user=uT;
            if(uT.Id==acc.OwnerId)
                user2=uT;
        }*/
       
        if(trigger.old[0].OwnerId!=trigger.new[0].OwnerId)
        {
            Profile prf = [Select p.Name, p.Id From Profile p where Name='System Administrator' or Name='系统管理员'  LIMIT 1 ];
            if (prf.Id != UserInfo.getProfileId())
            {
                User user;
                User user2;
                List<User> UserList=[select Id,Name,Title__c,ManagerId from User where Id=:UserInfo.getUserId() or Id=:acc.OwnerId];
                for(User uT:UserList)
                {
                    if(uT.Id==UserInfo.getUserId())
                        user=uT;
                    if(uT.Id==acc.OwnerId)
                        user2=uT;
                }
                
                if(user2.Title__c=='Sales Rep')
                {
                    if(user.Id != user2.ManagerId)
                    {
                        User u=[select Id,Name,ManagerId from User where Id=: user2.ManagerId];//SUP
                        
                        if(u.ManagerId != user.Id)
                        {
                            trigger.new[0].OwnerId.addError('您无更改此用户的权限');
                        }
                    } 
                }   
                else
                {
                    trigger.new[0].OwnerId.addError('您无更改此用户的权限');
                }
            }   
        }
    //}
}